package com.zhilianbao.leyaogo.ui.fragment.me.myaccount;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.bql.recyclerview.swipe.SwipeMenuCreator;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.model.response.me.HistoryResponse;
import com.zhilianbao.leyaogo.ui.adapter.me.HistoryAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.SwipeRefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.decoration.ListLineDecoration;

import java.util.List;

import okhttp3.Response;

/**
 * Created by liu on 2016/10/26.
 * 历史
 */
public class HistoryFragment extends SwipeRefreshAndLoadFragment<HistoryResponse> {
    HistoryAdapter mHistoryAdapter;

    public static HistoryFragment getInstance() {
        HistoryFragment historyFragment = new HistoryFragment();
        return historyFragment;
    }

    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public SwipeMenuAdapter getAdapter() {
        return mHistoryAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return new ListLineDecoration();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_balance_history;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public SwipeMenuCreator getSwipeMenuCreator() {
        return null;
    }

    @Override
    public void onLeftSwipeMenuClick(int adapterPosition, int menuPosition) {

    }

    @Override
    public void onRightSwipeMenuClick(int adapterPosition, int menuPosition) {

    }


    @Override
    public void onRefresh() {
        loadDataList(1, true);
    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        mCurPage = curPage;
        //这里只显示充值历史记录
        AccountApi.getFundRecordPageList(mActivity, Utils.getUserLoginInfo().getUserId(), Utils.getSupplierId(), 2, curPage, pageSize, new RefreshAndLoadCallback<List<HistoryResponse>>(isPullToRefresh) {
            @Override
            public void errorLeftOrEmptyBtnClick(View v) {
                TextView tv = (TextView) v;
                if (tv.getText().toString().trim().equals(emptyBtnText())) {
                    mActivity.onBackPressedSupport();
                } else {
                    loadDataList(1, false);
                }
            }

//            @Override
//            public void onResultSuccess(HistoryResponse historyResponse, @Nullable Response response, LoadingViewCallback callback) {
//
//                handleRefreshAndLoadHistoryListData(mCurPage, callback, historyResponse.getList(), historyResponse.getTotal());
//
//            }

            @Override
            public void onResultSuccess(List<HistoryResponse> historyResponses, @Nullable Response response, LoadingViewCallback callback) {
                int total = 0;
                if (historyResponses != null && historyResponses.size() != 0) {
                    total = historyResponses.get(0).getTotal();
                }
                handleRefreshAndLoadHistoryListData(mCurPage, callback, historyResponses, total);

            }

            @Override
            public String emptyContent() {
                return "主人,充值历史为空哦～";
            }

            @Override
            public boolean showEmptyButton() {
                return true;
            }

            @Override
            public String emptyBtnText() {
                return "去充值";
            }
        });
    }

    @Override
    public void onLoadMore() {
        loadDataList(mCurPage, true);
    }

    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.history));
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mHistoryAdapter = new HistoryAdapter(mActivity, mList);
    }
}
